<?php

namespace catchAdmin\apipatient\controller;

use think\Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use think\exception\ValidateException;
use catchAdmin\apipatient\validate\MydValidate;
use app\common\model\Project as ProjectModel;
use catchAdmin\registration\service\TaskService;

class Myd extends CatchController
{
    /**
     * 患者满意度列表
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_hzd_myd_list(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(MydValidate::class)->scene('get_hzd_myd_list')->check($req);
            return CatchResponse::paginate((new ProjectModel)->getMydDatas($req));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 推送满意度问卷
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function push_hzd_questionnaire(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(MydValidate::class)->scene('push_hzd_questionnaire')->check($req);
            $req['patientList'] = request()->userInfo['patientId'];
            return CatchResponse::success((new TaskService)->pushHzdQuestionnaire($req));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }


    public function getMyAnswer(Request $request)
    {
        $limit = $request->param("limit", 15, "int");
        $result = (new \app\common\model\Answer())->field(["id", "projectId", "name", "endTime", "questions.name", "questions.classification"])->where([["patient_id", "=", $request->userInfo["patientId"]], ["follow_id", "=", 0], ["answer", '<>', []]])->order("_id", "desc")->paginate($limit);
        if (count($result->items()) > 0) {
            $result->append(["end_time_text", "classificationText"])->hidden(["endTime"]);
        }
        return CatchResponse::paginate($result);
    }
}